<?php
/*
	author: Panos Sakkos - dkloz
	purpose: To add an entity to the category specified by its structure 
	Returns BL_OK upon success 
	Returns a proper fail code upon failure
	STATUS: IMPLEMENTED - NOT DEBUGGED
*/


//cateogyid, entityname, attrsvals
function BL_insertEntity($categoryId, $entityName, $Attrvals)
{
	$category = BL_getCategory($categoryId);
	
	/*if(is_null($category))
	{
		return BL_ERROR_IN_INSERT_ENTITY;
	}
	*/
	//check if an entity with that same name exists 
	$entity_ids = array();
	$entity_ids = DB_getEntityIds($categoryId);



	if( count($entity_ids, 0) != 0){
		foreach ($entity_ids as $entityId)
			{
				$temp_entity = DB_getEntity($entityId);
				if(!strcmp($temp_entity->GetName(),$entityName)){
					return BL_INSERT_ENT_SAME_NAME;
				}
			}
	}

	$attributeNames = $category->GetAttributes();

	$entity = new Entity();
	$entity->SetName($entityName);
	$entity->SetCategoryId($categoryId);

	$attributes = array();

	$i = 0;
	foreach($Attrvals as $attributeValue){
		$newAttr = new Attribute();
		$newAttr->setCategoryId($categoryId);
		$newAttr->setName($attributeNames[$i]->GetName());
		$newAttr->SetValue($attributeValue);
		$newAttr->SetType($attributeNames[$i]->GetType());
		$attributes[$i] = $newAttr;
		$i++;
	}

	$entity->setAttributes($attributes);
	
 	$return_value = DB_category_addentity($category,$entity);

	if ($return_value < 0)
	{
		return BL_ERROR_IN_INSERT_ENTITY;
	}
	else
	{
		return BL_OK;
	}
}
?>